package net.qhzw.vip.qiyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import net.qhzw.vip.qiyu.entity.DtsChargingPile;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.DtsChargingPileService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("sys/chargingPile")
@Api(tags = "充电")
public class SysChargingPileController {

    @Autowired
    private DtsChargingPileService chargingPileService;

    @GetMapping("/{id}")
    public JsonResult findOne(@PathVariable Long id) {
        DtsChargingPile chargingPile = chargingPileService.getById(id);
        return JsonResult.ok(chargingPile);
    }

    @GetMapping
    public JsonResult<Page<DtsChargingPile>> findByPage(Page<DtsChargingPile> page, String name, String address) {
        page.addOrder(OrderItem.desc("created"));
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Page<DtsChargingPile> chargingPilePage = chargingPileService
                .page(page, new LambdaQueryWrapper<DtsChargingPile>()
                        .eq(shopId != 1, DtsChargingPile::getShopId, shopId)
                        .like(!StringUtils.isEmpty(name), DtsChargingPile::getName, name)
                        .like(!StringUtils.isEmpty(address), DtsChargingPile::getAddress, address));
        return JsonResult.ok(chargingPilePage);
    }


    @GetMapping("/list")
    public JsonResult<Page<DtsChargingPile>> list(Page<DtsChargingPile> page, String keywords, Double longitude, Double latitude) {
        Page<DtsChargingPile> list = chargingPileService.findList(page, keywords, longitude, latitude);
        return JsonResult.ok(list);
    }


    @PostMapping
    public JsonResult add(@RequestBody DtsChargingPile chargingPile) {
        int count = chargingPileService.count(new LambdaQueryWrapper<DtsChargingPile>()
                .eq(DtsChargingPile::getName, chargingPile.getName()));
        if (count > 0) {
            throw new ApiException(chargingPile.getName() + " 已经存在！");
        }
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        chargingPile.setShopId(shopId);
        chargingPile.setId(IdWorker.getId());
        boolean save = chargingPileService.save(chargingPile);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @PutMapping
    public JsonResult update(@RequestBody DtsChargingPile chargingPile) {
        int count = chargingPileService.count(new LambdaQueryWrapper<DtsChargingPile>()
                .eq(DtsChargingPile::getName, chargingPile.getName()));
        if (count > 1) {
            throw new ApiException(chargingPile.getName() + " 已经存在！");
        }
        boolean update = chargingPileService.updateById(chargingPile);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @DeleteMapping("/{id}")
    public JsonResult del(@PathVariable String id) {
        List<String> ids = Arrays.asList(id.split(","));
        boolean isOK = chargingPileService.removeByIds(ids);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

}
